using System;
using System.Globalization;
using System.Text;

namespace BizTalk.BuildGenerator.CommandBuilders
{
    /// <summary>
    /// Creates a command to publish web services from the build process
    /// </summary>
    public class PublishedWebServicesCommandBuilder : ICommandBuilder
    {
        private const string PublishSoapWebServiceCommand =
            "\t\t<BizTalk.BuildGenerator.Tasks.WebServiceGenerator WebServiceDescriptionPath=\"{0}\" />";

        private const string PublishWse2WebServiceCommand =
            "\t\t<BizTalk.BuildGenerator.Tasks.WseWebServiceGenerator WebServiceDescriptionPath=\"{0}\" />";

        private const string PublishWcfServiceCommand =
            "\t\t<BizTalk.BuildGenerator.Tasks.WcfServiceGenerator DescriptionPath=\"{0}\" />";

        private const string CommandTag = "<!-- @PublishWebServicesCommand@ -->";

        #region ICommandBuilder Members

        /// <summary>
        /// Implements the build method
        /// </summary>
        /// <param name="args"></param>
        /// <param name="fileBuilder"></param>
        public void Build(GenerationArgs args, StringBuilder fileBuilder)
        {
            StringBuilder commandBuilder = new StringBuilder();

            foreach (PublishedWebService ws in args.WebServices.WebServices)
            {
                string command;
                if (ws.Type == WebServiceTypes.Soap)                
                    command = string.Format(CultureInfo.InvariantCulture, PublishSoapWebServiceCommand, ws.WebServiceDescriptionPath);                

                else if (ws.Type == WebServiceTypes.Wse2)                
                    command = string.Format(CultureInfo.InvariantCulture, PublishWse2WebServiceCommand, ws.WebServiceDescriptionPath);
                
                else if (ws.Type == WebServiceTypes.Wcf)                
                    command = string.Format(CultureInfo.InvariantCulture, PublishWcfServiceCommand, ws.WebServiceDescriptionPath);
                
                else
                    break;

                commandBuilder.Append(command);
                commandBuilder.Append(Environment.NewLine);
            }

            fileBuilder.Replace(CommandTag, commandBuilder.ToString());
        }

        #endregion
    }
}